import { md5 } from './md5';
import { sortObjectProp } from './util';
import qs from 'qs';
import Http from '@/utils/http';

/**
 * 转换输出的参数
 * @param params
 * @param sign
 */
export function packSignParams(params: any, sign?: (data: string) => string) {
  const { timestamp, method, ...extraParams } = params;
  const data = JSON.stringify(extraParams);
  const v = encodeURIComponent(sign ? sign(data) : data);
  const signParams: Record<string, any> = { v };
  if (params) {
    if (!timestamp) {
      signParams.t = Date.now().toFixed(0);
    }
    if (!method) {
      signParams.m = '100021';
    }
  }
  const o = sortObjectProp(signParams, false);
  console.log(o);
  const str = Http.buildQuery(o);
  console.log(str);
  signParams.s = mkSign(str, signParams.method);
  return signParams;
}

/**
 * 生成签名
 * @param data
 * @param _method
 * @returns {*}
 */
export function mkSign(data: any, _method = 'md5') {
  const method = _method.toUpperCase();
  switch (method) {
    case 'HMAC256':
      return '';
    default:
      return md5(data);
  }
}
